package com.fastloan.mgt.postloan.web;

import com.fastloan.mgt.postloan.model.ZyPostLoanMonitor;
import com.fastloan.mgt.postloan.model.ZyPostloanFile;
import com.fastloan.mgt.postloan.model.vo.PostLoanMonitorExt;
import com.fastloan.mgt.postloan.service.ZyPostLoanMonitorService;
import com.fastloan.mgt.postloan.service.ZyPostloanFileService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.sys.model.CmuSysFile;
import com.fastloan.safe.mgt.sys.service.CmSysFileService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by gelon on 2017/11/17.
 */
@RestController
@RequestMapping("/api/zy/web/postloanMonitor")
public class PostloanWebMonitorController extends BaseController<ZyPostLoanMonitor> {

    @Autowired
    ZyPostLoanMonitorService postLoanMonitorService;

    @Autowired
    ZyPostloanFileService postloanFileService;

    @Autowired
    CmSysFileService fileService;

    /**
     * 获取客户经理下进件的贷后实施信息
     */
    @PostMapping()
    public ResultTo postloans(String customerName, String productName,Integer postLoanImplementResult) {
        Map<String, Object> map = new HashMap<>();
        map.put("managerId", ManagerKit.getUserId());
        if (customerName != null) {
            map.put("customerName", customerName);
        }
        if (productName != null) {
            map.put("productName", productName);
        }
        if( postLoanImplementResult != null){
            map.put("postLoanImplementResult",postLoanImplementResult);
        }
        return dataPage(postLoanMonitorService, PostLoanMonitorExt.class, "queryPostloan", map);
    }


    /**
     * 实施贷后
     */
    @PostMapping("/implementPostloan")
    public ResultTo implementPostloan(ZyPostLoanMonitor postLoanMonitor, MultipartFile[] files) {
        postLoanMonitorService.uploadPostloan(postLoanMonitor, files);
        return new ResultTo();
    }

    /**
     * 单个贷后信息
     */
    @GetMapping("/{postloanId}")
    public ResultTo postloanInfo(@PathVariable String postloanId) {
        ZyPostLoanMonitor postLoanMonitor = postLoanMonitorService.selectByPrimaryKey(postloanId);
        List<CmuSysFile> files = postloanFileService
            .select(new ZyPostloanFile().setPostLoanId(postloanId)).stream()
            .map(zyPostloanFile -> fileService.selectByPrimaryKey(zyPostloanFile.getFileId()))
            .collect(Collectors.toList());
        return new ResultTo().setData("postloanMonitor", postLoanMonitor).setData("files", files);
    }


}
